<?php
/**
 * 公共空间租赁模块微站定义
 *
 * @author 橙橙同学
 * @url 
 */
defined('IN_IA') or exit('Access Denied');

class Orange_spacerentalModuleSite extends WeModuleSite {

    public function doMobileDevCommonTipsSuccess()
    {
        include $this->template('commontips/success');
    }
    public function doMobileDevCommonTipsError()
    {
        include $this->template('commontips/error');
    }
    public function doMobileDevSpaceList()
    {
        include $this->template('spacerental/spacelist');
    }
    public function doMobileDevSpaceRentalApply()
    {
        include $this->template('spacerental/apply');
    }
    public function doMobileDevSpaceRentalOrderDetail()
    {
        include $this->template('spacerental/orderdetail');
    }
    public function doMobileDevSpaceRentalOrderRecord()
    {
        include $this->template('spacerental/orderrecord');
    }


//    public function __construct()
//    {
//        $this->commonCheckOpenid();
//    }


    /**
     * @param $message
     * @author:橙橙同学
     * @func:公共提示
     */
    protected function commonTips($message,$redirect = '',$type)
    {
        if($redirect == '')
        {
            $redirectUrl = $_SERVER['HTTP_REFERER'];
        }else{
            $redirectUrl = $redirect;
        }
        switch ($type){
            case "success":
                message($message,$redirectUrl,'success');
//                $this->template('commontips/success');
                break;
            case "info":
                message($message,$redirectUrl,'info');
//                $this->template('commontips/success');
                break;
            case "error":
                message($message,$redirectUrl,'error');
//                $this->template('commontips/error');
                break;
            default:
                message($message,$redirectUrl,'success');
//                $this->template('commontips/success');
        }

    }

    protected function commonApiCheckOpenid($openid)
    {
        if(empty($openid))
        {
            $this->commonApiReturn(-1,'','请在微信端打开本页面');
            exit();
        }
    }

    protected function commonApicheckUid($uid)
    {
        if(empty($uid))
        {
            $this->commonApiReturn(-1,'','您还不是会员');
            exit();
        }
    }


    /**
     * @author:橙橙同学
     * @func:公共检测微信
     */
    protected function commonCheckOpenid()
    {
        global $_W;
        $openid = $_W['openid'];
        if(empty($openid))
        {
            $this->commonTips('请确保在微信端打开本页面','','info');
            exit();
        }
    }


    /**
     * @param $openid
     * @author:橙橙同学
     * @func:公共检测会员
     */
    protected function commonCheckUid($uid)
    {
        if(empty($uid))
        {
            $this->commonTips('您还不是会员','','info');
            exit();
        }
    }

    /**
     * @author:橙橙同学
     * @func:格式化API返回信息
     */
    protected function commonApiReturn($errCode = 0,$data = '',$msg = '')
    {
        $return = array(
            'errCode'=>$errCode,
            'data'=>$data,
            'msg'=>$msg
        );
        echo json_encode($return);
        exit();
    }


    /**
     * @param $uid
     * @return bool
     * @author:橙橙同学
     * @func:获取会员信息
     */
    protected function getMemberInfoByUid($uid)
    {
        global $_W;
        $memberInfo = pdo_fetch("select * from ".tablename('mc_members')." where `uniacid`=:uniacid
        and `uid`=:uid",array(':uniacid'=>$_W['uniacid'],':uid'=>$uid));
        return $memberInfo;
    }


    protected function spaceRentalDayMap()
    {
        //今天是几月几日
        $currentDay = date('Y-m-d');
        $currentTimeStamp = strtotime($currentDay);
        $timeMap = array(date("y-m-d",$currentTimeStamp));
        for($i=1;$i<7;$i++)
        {
            $nextDay = $currentTimeStamp + 24*3600*$i;
            $timeMap[$i] = date('Y-m-d',$nextDay);
        }
        return $timeMap;
    }

    /**
     * @author:橙橙同学
     * @func:空间租赁日期地图API
     */
    public function doMobileApiSpaceRentalDayMap()
    {
        //今天是几月几日
        $currentDay = date('Y-m-d');
        $currentTimeStamp = strtotime($currentDay);

        $weekArr=array("周日","周一","周二","周三","周四","周五","周六");
        $timeMap = array(
            array(
                'day'=>date("Y-m-d",$currentTimeStamp),
                'week'=>$weekArr[date("w",$currentTimeStamp)]
            )
        );
        for($i=1;$i<7;$i++)
        {
            $nextDay = $currentTimeStamp + 24*3600*$i;
            $timeMap[$i]['day'] = date('Y-m-d',$nextDay);
            $timeMap[$i]['week'] = $weekArr[date("w",$nextDay)];
        }
        if(!empty($timeMap)){
            $this->commonApiReturn(0,$timeMap,'获取日期成功');
        }else{
            $this->commonApiReturn(-1,'','获取日期失败');
        }
    }



    public function getSpaceNameById($space_id)
    {
        global $_W;
        $list = pdo_fetch("select * from ".tablename('spacerental_space')."where `uniacid`=:uniacid and
                       `id`=:id and status=1
                        ",
            array(':uniacid'=>$_W['uniacid'],':id'=>$space_id)
        );

        if(!empty($list)){
            return $list['space_name'];
        }else{
            return '无';
        }
    }

    /**
     * @author:橙橙同学
     * @func:空间管理
     */
    public function doWebSpacemanage()
    {
        global $_W,$_GPC;
        $op = $_GPC['op']?$_GPC['op']:'display';
        switch ($op){
            case 'display':
                $list = pdo_fetchall("select * from ".tablename('spacerental_space')."where `uniacid`=:uniacid
                        ",
                    array(':uniacid'=>$_W['uniacid'])
                );
                break;
            case 'delete':
                //删除 传递 产品主键id 对应租赁空间id
                if($_W['isajax'] && $_W['ispost'] && $_GPC['pro_id'])
                {
                    $pro_id = intval($_GPC['pro_id']);

                    $updateRes = pdo_update('spacerental_spaceproduct',array('status'=>-1),
                        array('uniacid'=>$_W['uniacid'],'id'=>$pro_id)
                    );
                    if($updateRes){
                        $this->commonApiReturn(0,'','删除成功');
                    }else{
                        $this->commonApiReturn(-1,'','删除失败');
                    }
                }else{
                    $this->commonApiReturn(-1,'','非法请求');
                }

                break;

            case 'change_status':

                if($_W['isajax'] && $_GPC['id'])
                {
                    $space_id = intval($_GPC['id']);
                    $status  = intval($_GPC['status']);

                    $updateRes = pdo_update('spacerental_space',array('status'=>$status),
                        array('uniacid'=>$_W['uniacid'],'id'=>$space_id)
                    );
                    if($updateRes){
                        $this->commonApiReturn(0,'','更新成功');
                    }else{
                        $this->commonApiReturn(-1,'','更新失败');
                    }
                }else{
                    $this->commonApiReturn(-1,'','非法请求');
                }

                break;

            case 'edit':

                $space_id = $_GPC['space_id'];
                $spaceInfo = pdo_fetch("select * from ".tablename('spacerental_space')." where `uniacid`=:uniacid
                        and `id`=:space_id and status!=-1
                        ",
                    array(':uniacid'=>$_W['uniacid'],':space_id'=>$space_id)
                );

                if (checksubmit('submit'))
                {
                    $space_name = trim($_GPC['space_name']);
                    if(!empty($space_name) && $space_name!=$spaceInfo['space_name'])
                    {
                        $space = pdo_fetch("select * from ".tablename('spacerental_space')."where `uniacid`=:uniacid
                        and `space_name`=:space_name  and status!=-1 and `type_id`=:type_id and `id`!={$space_id}
                        ",
                            array(':uniacid'=>$_W['uniacid'],':space_name'=>$space_name,':type_id'=>$_GPC['type_id'])
                        );
                        if(empty($space))
                        {
                            $data = array(
                                'space_name'=>$space_name,
                                'have_projection'=>$_GPC['have_projection'],
                                'have_sound'=>$_GPC['have_sound'],
                                'have_drinking'=>$_GPC['have_drinking'],
                                'seats_number'=>$_GPC['seats_number'],
                                'space_pic'=>$_GPC['space_pic'],
                                'type_id'=>$_GPC['type_id'],
                                'space_address'=>$_GPC['space_address'],
                                'space_intro'=>$_GPC['space_intro'],
                                'pay_type'=>$_GPC['pay_type'],
                                'charge_type'=>$_GPC['charge_type'],
                                'isfree'=>$_GPC['isfree'],
                                'uniacid'=>$_W['uniacid']
                            );
                            //插入空间表
                            $updateRes = pdo_update('spacerental_space',$data,array('id'=>$space_id));
                            if($updateRes)
                            {
                                $this->commonTips('更新成功','','success');
                            }else{
                                $this->commonTips('更新失败','','success');
                            }

                        }else{
                            $this->commonTips('此租赁空间已经存在','','info');
                        }
                    }
                    else{
                        $this->commonTips('更新失败','','info');
                    }

                }else{
                    //空间分类列表
                    $typeList = pdo_fetchall("select * from ".tablename('spacerental_spacetype')." where `uniacid`=:uniacid
                    and status=1
                    ",
                        array(':uniacid'=>$_W['uniacid'])
                    );
                }
                break;
            case 'add':
                if (checksubmit('submit'))
                {
                    $space_name = trim($_GPC['space_name']);
                    if(!empty($space_name))
                    {
                        // 名字相同并且开放间隔时间相同 视为重复空间
                        $space = pdo_fetch("select * from ".tablename('spacerental_space')."where `uniacid`=:uniacid
                        and `space_name`=:space_name and `charge_time`=:charge_time and status!=-1 and `type_id`=:type_id
                        ",
                            array(':uniacid'=>$_W['uniacid'],':space_name'=>$space_name,':charge_time'=>$_GPC['charge_time'],':type_id'=>$_GPC['type_id'])
                        );
                        if(empty($space))
                        {
                            $data = array(
                                'space_name'=>$space_name,
                                'have_projection'=>$_GPC['have_projection'],
                                'have_sound'=>$_GPC['have_sound'],
                                'have_drinking'=>$_GPC['have_drinking'],
                                'seats_number'=>$_GPC['seats_number'],
                                'space_pic'=>$_GPC['space_pic'],
                                'type_id'=>$_GPC['type_id'],
                                'space_address'=>$_GPC['space_address'],
                                'space_intro'=>$_GPC['space_intro'],
                                'pay_type'=>$_GPC['pay_type'],
                                'charge_type'=>$_GPC['charge_type'],
                                'charge_time'=>$_GPC['charge_time'],
                                'isfree'=>$_GPC['isfree'],
                                'uniacid'=>$_W['uniacid']
                            );
                            //如果是按次数计算费用的空间 则默认是24小时 不需要添加对应开放时间
                            if($_GPC['pay_type'] == 2 ){

                                $insertRes = pdo_insert('spacerental_space',$data);
                                if($insertRes)
                                {
                                    $this->commonTips('添加成功','','success');
                                }


                            }else{

                                pdo_begin();
                                try{
                                    //插入空间表
                                    $insertRes = pdo_insert('spacerental_space',$data);
                                    if(!$insertRes)
                                    {
                                        throw new Exception('添加空间数据失败');
                                    }
                                    //拿到空间主键ID
                                    $insertId = pdo_insertid();
                                    foreach ($_GPC['sel_time'] as $key=>$value){
                                        $proData = array(
                                            'space_id'=>$insertId,
                                            'pro_name'=>$value,
                                            'create_time'=>time(),
                                            'price'=>$_GPC['price'],
                                            'uniacid'=>$_W['uniacid']
                                        );

                                        $insertR = pdo_insert('spacerental_spaceproduct',$proData);
                                        if(!$insertR)
                                        {
                                            throw new Exception('添加空间产品数据失败');
                                        }
                                    }
                                    pdo_commit();
                                    $this->commonTips('添加成功','','success');
                                }catch (Exception $e){
                                    $this->commonTips('添加失败'.$e->getMessage(),'','error');
                                }
                            }

                        }else{
                            $this->commonTips('此租赁空间已经存在','','info');
                        }
                    }
                    else{
                        $this->commonTips('添加失败','','info');
                    }

                }else{
                    //空间分类列表
                    $typeList = pdo_fetchall("select * from ".tablename('spacerental_spacetype')." where `uniacid`=:uniacid
                    and status=1
                    ",
                        array(':uniacid'=>$_W['uniacid'])
                    );
                }
                break;
            default:
        }
        include $this->template('spacemanage/spacemanage');
    }


    /**
     * @author:橙橙同学
     * @func:空间分类管理
     */
    public function doWebSpacerentalSpaceTypeManage()
    {
        global $_W,$_GPC;
        $op = $_GPC['op']?$_GPC['op']:'display';
        $return['errCode'] = 0;

        switch ($op){
            case 'display':
                $list = pdo_fetchall("select * from ".tablename('spacerental_spacetype')."where `uniacid`=:uniacid
                    and status=1
                        ",
                    array(':uniacid'=>$_W['uniacid'])
                );
                break;
            case 'delete':
                if($_W['isajax'] && $_W['ispost'])
                {
                    $type_id = intval($_GPC['type_id']);
                    $updateRes = pdo_update('spacerental_spacetype',array('status'=>-1),
                        array('uniacid'=>$_W['uniacid'],'id'=>$type_id)
                    );
                    if($updateRes){
                        $this->commonApiReturn(0,'','删除成功');
                    }else{
                        $this->commonApiReturn(-1,'','删除失败');
                    }
                }else{
                    $this->commonApiReturn(-1,'','非法请求');
                }
                break;
            case 'edit':
                $value = $_GPC['value'];
                $id = $_GPC['id'];
                $data = array('type_name'=>$value);

                $res = pdo_update('spacerental_spacetype',$data,array('id'=>$id,'uniacid'=>$_W['uniacid']));
                if($res){
                    $this->commonApiReturn(0,'','更新成功');
                }else{
                    $this->commonApiReturn(-1,'','更新失败');
                }
                break;
            case 'add':
                if($_W['isajax'] && $_W['ispost'])
                {
                    $type_name = trim($_GPC['type_name']);
                    if(!empty($type_name))
                    {
                        $spaceType = pdo_fetch("select * from ".tablename('spacerental_spacetype')." 
                        where `uniacid`=:uniacid
                        and `type_name`=:type_name and status=1
                        ",
                            array(':uniacid'=>$_W['uniacid'],':type_name'=>$type_name)
                        );
                        if(empty($spaceType))
                        {
                            $data = array(
                                'type_name'=>$type_name,
                                'create_time'=>time(),
                                'uniacid'=>$_W['uniacid']
                            );

                            $insertRes = pdo_insert('spacerental_spacetype',$data);
                            if($insertRes)
                            {
                                $this->commonApiReturn(0,'','添加成功');
                            }
                            else{
                                $this->commonApiReturn(-1,'','添加失败');
                            }
                        }else{
                            $this->commonApiReturn(-1,'','该租赁空间此商品已经存在');
                        }
                    }
                    else{
                        $this->commonApiReturn(-1,'','添加失败');
                    }
                }
                break;
            default:
        }
        include $this->template('typemanage/typemanage');
    }

    public function doWebSpaceProManage()
    {
        global $_W,$_GPC;
        $op = $_GPC['op']?$_GPC['op']:'display';
        switch ($op) {
            case 'editpro':
                $space_id = $_GPC['space_id'];
                if(!empty($space_id))
                {
                    $sql = "select * from ".tablename('spacerental_spaceproduct')." where `uniacid`=:uniacid and `space_id`=:space_id
                    ";
                    $spacePros = pdo_fetchall($sql,array(':uniacid'=>$_W['uniacid'],'space_id'=>$space_id));
                }else{
                    $this->commonTips('space_id为空','','ifno');
                }
                break;
            case 'prostatus':
                $pro_id = $_GPC['pro_id'];
                $status = $_GPC['status'];

//                $sql = "select * from ".tablename('spacerental_spaceproduct')." where `uniacid`=:uniacid and `id`=:pro_id
//                    ";
//                $proInfo = pdo_fetch($sql,array(':uniacid'=>$_W['uniacid'],':pro_id'=>$pro_id));
//
//                if($proInfo['status'] == $status)
//                {
//                    $this->commonApiReturn(4403,'','');
//                }

                if(!empty($pro_id) && $pro_id!='')
                {
                    $data = array(
                        'status'=>$status
                    );
                    $updateRes = pdo_update('spacerental_spaceproduct',$data,array('id'=>$pro_id,'uniacid'=>$_W['uniacid']));


                    if($updateRes)
                    {
                        $this->commonApiReturn(0,'','更新成功');
                    }
                    else{
                        $this->commonApiReturn(-1,'','更新失败，请稍后再试');
                    }
                }else{
                    $this->commonApiReturn(-1,'','非法操作');
                }


            default:
        }
        include $this->template('spacepromanage/spacepromanage');
    }

    public function doWebSpaceRentalProductManage()
    {
        global $_W,$_GPC;
        $op = $_GPC['op']?$_GPC['op']:'display';
        switch ($op){
            case 'display':
                //产品列表
                $list = pdo_fetchall("select * from ".tablename('spacerental_spaceproduct')."where `uniacid`=:uniacid
                       and status=1
                        ",
                    array(':uniacid'=>$_W['uniacid'])
                );
                break;
            case 'delete':
                //删除 传递 产品主键id 对应租赁空间id
                if($_W['isajax'] && $_W['ispost'] && $_GPC['pro_id'])
                {
                    $pro_id = intval($_GPC['pro_id']);

                    $updateRes = pdo_update('spacerental_spaceproduct',array('status'=>-1),
                        array('uniacid'=>$_W['uniacid'],'id'=>$pro_id)
                    );
                    if($updateRes){
                        $this->commonApiReturn(0,'','删除成功');
                    }else{
                        $this->commonApiReturn(-1,'','删除失败');
                    }

                    /*
                    //某租赁空间下的某产品
                    $pro = pdo_fetch("select * from ".tablename('spacerental_spaceproduct')." where `uniacid`=:uniacid
                        and `space_id`=:space_id and `id`=:pro_id and `status`=1
                        ", array(':uniacid'=>$_W['uniacid'],':space_id'=>$space_id,':pro_id'=>$pro_id)
                    );

                    if(!empty($pro))
                    {
                        $updateRes = pdo_update('spacerental_spaceproduct',array('status'=>-1),
                            array(':uniacid'=>$_W['uniacid'],':id'=>$pro_id,':space_id'=>$space_id)
                        );
                        if($updateRes){
                            $this->commonApiReturn(-1,'','删除成功');
                        }else{
                            $this->commonApiReturn(-1,'','删除失败');
                        }
                    }else{
                        $this->commonApiReturn(-1,'','删除失败');
                    }
                    */
                }else{
                    $this->commonApiReturn(-1,'','非法请求');
                }

                exit();
                break;
            case 'edit':
                $value = $_GPC['type_name'];
                $id = $_GPC['id'];

                $data = array('type_name'=>$value);

                $res = pdo_update('storemanage_sorts',$data,array('id'=>$id,'uniacid'=>$_W['uniacid']));
                if($res){
                    $return['msg'] = '更新成功';
                }else{
                    $return['errCode'] = -1;
                    $return['msg'] = '更新失败';
                }
                echo json_encode($return);
                exit();
                break;
            case 'add':
                if($_W['isajax'] && $_W['ispost'])
                {
                    $space_id = $_GPC['space_id'];
                    $pro_name = trim($_GPC['pro_name']);
                    if(!empty($space_id) && !empty($pro_name))
                    {
                        //判断是否存在此产品
                        $pro = pdo_fetch("select * from ".tablename('spacerental_spaceproduct')."where `uniacid`=:uniacid
                        and `space_id`=:space_id and `pro_name`=:pro_name and status=1
                        ",
                            array(':uniacid'=>$_W['uniacid'],':space_id'=>$space_id,':pro_name'=>$pro_name)
                        );
                        if(empty($pro))
                        {

                            $data = array(
                                'space_id'=>$space_id,
                                'pro_name'=>$pro_name,
                                'create_time'=>time(),
                                'uniacid'=>$_W['uniacid']
                            );
                            //插入产品表
                            $insertRes = pdo_insert('spacerental_spaceproduct',$data);
                            if($insertRes)
                            {
                                $this->commonApiReturn(0,'','添加成功');
                            }
                            else{
                                $this->commonApiReturn(-1,'','添加失败');
                            }

                        }else{
                            $this->commonApiReturn(-1,'','该租赁空间此商品已经存在');
                        }
                    }
                    else{
                        $this->commonApiReturn(-1,'','添加失败');
                    }
                    exit();
                }else{
                    //空间列表
                    $spaceList = pdo_fetchall("select * from ".tablename('spacerental_space')." where `uniacid`=:uniacid
                    and status=1
                    ",
                        array(':uniacid'=>$_W['uniacid'])
                    );

                }
                break;
            default:
        }
        include $this->template('productmanage/productmanage');
    }


    /**
     * @author:橙橙同学
     * @func:获取租赁类型列表
     */
    public function doMobileApiGetSpaceListByType(){

        global $_W,$_GPC;
        $type_id = $_GPC['type_id'];

        if(!empty($type_id))
        {
            $spaceList = pdo_fetchall("select * from ".tablename('spacerental_space')." where `uniacid`=:uniacid and `status`!=-1
                and `type_id`=:type_id",
                array(':uniacid'=>$_W['uniacid'],':type_id'=>$type_id)
            );

            if(!empty($spaceList)){
                $this->commonApiReturn(0,$spaceList,'获取成功');
            }else{
                $this->commonApiReturn(-1,'','请求成功但没有数据');
            }
        }else{
            $this->commonApiReturn(-1,'','参数不能为空');
        }

    }


    /**
     * @author:橙橙同学
     * @func:空间租赁列表页
     */
    public function  doMobileSpaceList()
    {
        global $_W;
        $spaceTypeList = pdo_fetchall("select * from ".tablename('spacerental_spacetype')." where `uniacid`=:uniacid and `status`=1",
            array(':uniacid'=>$_W['uniacid'])
        );
        include $this->template('spacerental/spacelist');
    }


    /**
     * @author: 橙橙同学
     * @fun:检测字段是否为空
     */
    protected function commconCheckEmptyFields($data,$checkArr)
    {
        foreach ($checkArr as $key=>$value)
        {
            if(empty($data[$key]))
            {
                $this->commonApiReturn(-1,'',$value.'不能为空');
            }
        }
    }

    /**
     * @author: 橙橙同学
     * @fun:检查企业会员身份
     */
    protected function commonCheckCompanyMember($memberInfo)
    {
        if($memberInfo){
            if($memberInfo['groupid'] == 3)
            {
                return true;
            }
            else{
                return false;
            }
        }else{
            return false;
        }
    }



    protected function checkRentalPro($apply_hours,$space_id)
    {
        global $_W;
        $list = pdo_fetchall("select id from ".tablename('spacerental_spaceproduct')."where `uniacid`=:uniacid and
                                   `space_id`=:space_id and status=1
                                    ",
            array(':uniacid'=>$_W['uniacid'],':space_id'=>$space_id)
        );
        foreach ($list as $lk=>$lv)
        {
            $proListIds[] = $lv['id'];
        }
        //得到差集
        $arrdiff = array_diff($apply_hours,$proListIds);

        if(!empty($arrdiff))
        {
            $this->commonApiReturn(-1,'','申请的租赁时间不合法');
        }
    }




    protected function checkRentalStatus($space_id,$apply_day,$apply_hours)
    {
        global $_W;
        //下单前安全性判断 查询该用户租赁的空间，在他选的日期 和时间 是否被租赁掉了
        $sql = "select detail.pro_id from `ims_spacerental_order` as orders,
                        `ims_spacerental_orderdetail` as detail where `orders`.uniacid=:uniacid and 
                        `orders`.space_id=:space_id and `orders`.status!=-1 
                        and `orders`.id=detail.order_id and orders.day=:day";

        //该租赁空间 当日已经租出去的产品id
        $orderidArr = pdo_fetchall($sql,
            array(':uniacid' => $_W['uniacid'], ':space_id' => $space_id, ':day' => $apply_day)
        );

        if (!empty($orderidArr)) {
            foreach ($orderidArr as $key => $value) {
                $orderIds[] = $value['pro_id'];
            }
            //拿到顾客租赁的产品数组id与被租出去的产品数组id的交集
            $res = array_intersect($orderIds,$apply_hours);

            if(!empty($res))
            {

                $isselPro = pdo_fetchall("select pro_name from ".tablename('spacerental_spaceproduct')." where `uniacid`=:uniacid and 
                id in (".implode(',',$res).")",array(':uniacid'=>$_W['uniacid']));


                foreach ($isselPro as $k=>$v)
                {
                    $selPro[] = $v['pro_name'];
                }
                //截断 订单中有产品已经被租
                $str = implode(',',$selPro);
                $this->commonApiReturn(-1,'',"对不起 您申请的时间段".$str."已经被出租");
            }
        }
    }




    /**
     * @author:橙橙同学
     * @func:根据space_id day 获取产品列表状态
     */
    public function doMobileApiGetProBySpaceId()
    {
        global $_W,$_GPC;
        $space_id = $_GPC['space_id'];
        $day = $_GPC['day'];
        if(!empty($space_id) && !empty($day))
        {
            $list = pdo_fetchall("select id,pro_name,price from ".tablename('spacerental_spaceproduct')."where `uniacid`=:uniacid and
                           `space_id`=:space_id and status=1
                            ",
                array(':uniacid'=>$_W['uniacid'],':space_id'=>$space_id)
            );
            if(!empty($list)) {
                //联表查询space_id对应的订单号以及订单号对应的订单中所出租的产品id集合
                // 1 待审核 2 预约成功 -1 预约失败 -2已使用 -3 已过期
                $sql = "select detail.pro_id from `ims_spacerental_order` as orders,
                    `ims_spacerental_orderdetail` as detail where `orders`.uniacid=:uniacid and 
                    `orders`.space_id=:space_id and `orders`.status!=-1 
                    and `orders`.id=detail.order_id and orders.day=:day";
                //该租赁空间 当日已经租出去的产品id
                $orderidArr = pdo_fetchall($sql,
                    array(':uniacid' => $_W['uniacid'], ':space_id' => $space_id, ':day' => $day)
                );
                if (!empty($orderidArr)) {
                    foreach ($orderidArr as $key => $value) {
                        $orderIds[] = $value['pro_id'];
                    }
                    //标记已经租赁出去的产品
                    foreach ($list as $k => $v) {
                        if (in_array($v['id'], $orderIds)) {
                            $list[$k]['issel'] = 1;
                        } else {
                            $list[$k]['issel'] = 0;
                        }
                    }
                }else{
                    //没有一个租出去的 全部标记为未租
                    foreach ($list as $k => $v) {
                        $list[$k]['issel'] = 0;
                    }
                }
                //如果数组里面 issel全部是1 那么就都租掉了
                $mark = false;
                foreach ($list as $ks=>$vs){
                    if($vs['issel'] == 0)
                    {
                        $mark = true;
                    }
                }
                $this->commonApiReturn(0, $list, '成功');
                /*
                if($mark)
                {
                    $this->commonApiReturn(0, $list, '成功');
                }else{
                    $this->commonApiReturn(0, $list, '该空间今日已经全部出租,请选择其他日期');
                }
                */
            }
            else{
                $this->commonApiReturn(4404, '', '没有该租赁空间对应可出售的产品');
            }
        }else{
            $this->commonApiReturn(-1, '', '参数不能为空');
        }
    }



    /**
     * @author: 橙橙同学
     * @fun:租赁申请API
     */
    public function doMobileApiSpaceRentalApply()
    {
        global $_W,$_GPC;
        $openid = $_W['openid'];
        $this->commonApiCheckOpenid($openid);
        $uid = mc_openid2uid($openid);
        $this->commonApicheckUid($uid);
        $memberInfo = $this->getMemberInfoByUid($uid);
        $isCompanyMember = $this->commonCheckCompanyMember($memberInfo);

        if(!$isCompanyMember)
        {
            $this->commonApiReturn(-1,'','系统检测到您不是企业会员');
        }
        $space_id = $_GPC['space_id'];
        if(empty($space_id)){
            $this->commonApiReturn(-1,'','租赁空间id为空');
        }

        $spaceInfo = pdo_fetch("select * from ".tablename('spacerental_space')." 
                    where `uniacid`=:uniacid and `id`=:id and status=1",
            array(':uniacid'=>$_W['uniacid'],':id'=>$space_id)
        );

        if(empty($spaceInfo))
        {
            $this->commonApiReturn(-1,'','未找到租赁空间');
        }

        //按时间计费生成订单
        if($spaceInfo['pay_type'] == 1)
        {
            $this->spaceRentalApplyByTime($memberInfo);
        }else {
            $this->spaceRentalApplyByTimes($memberInfo);
        }

    }


    /**
     * @author:橙橙同学
     * @func:空间租赁申请 按次数
     */
    public function spaceRentalApplyByTimes($memberInfo)
    {
        global $_W,$_GPC;
        $uid = $memberInfo['uid'];
        $checkArr = array(
            'apply_content'=>'申请事由',
            'apply_name'=>'申请人姓名',
            'apply_mobile'=>'申请人手机号',
            'apply_remark'=>'申请备注',
            'apply_day'=>'申请日',
            'space_id'=>'租赁空间id'
        );

        //字段是否为空判断
        $this->commconCheckEmptyFields($_GPC,$checkArr);
        $apply_content = $_GPC['apply_content'];
        $apply_name = $_GPC['apply_name'];
        $apply_mobile = $_GPC['apply_mobile'];
        $apply_remark = $_GPC['apply_remark'];
        $apply_day = $_GPC['apply_day'];
        $space_id = $_GPC['space_id'];

        $spaceInfo = pdo_fetch("select * from ".tablename('spacerental_space')." where `uniacid`=:uniacid and `id`=:space_id",
            array(
                ':uniacid'=>$_W['uniacid'],
                ':space_id'=>$space_id
            ));

        $date = date('YmdHis');
        $space_id = $space_id;

        //时间精确到秒 空间id 用户uid 合起来组成 订单号
        $orderno = $date.$space_id.$uid;

        $orderData = array(
            'apply_content'=>$apply_content,
            'apply_name'=>$apply_name,
            'apply_mobile'=>$apply_mobile,
            'apply_remark'=>$apply_remark,
            'space_id'=>$space_id,
            'uid'=>$uid,
            'uniacid'=>$_W['uniacid'],
            'day'=>$apply_day,
            'pro_ids'=>'按次数计费任意时段',
            'pro_names'=>'按次数计费任意时段',
            'space_type'=>$spaceInfo['type_id'],
            'create_time'=>time(),
            'order_no'=>$orderno
//                    'status'=>1, 默认为1 待审核
        );
        $orderInsert = pdo_insert('spacerental_order',$orderData);
        $order_id = pdo_insertid();
        $data = array(
            'order_id'=>$order_id
        );
        if($orderInsert)
        {
            $this->commonApiReturn(0,$data,'申请成功，等待审核');
        }else{
            $this->commonApiReturn(-1,'','申请失败');
        }
    }

    /**
     * @author:橙橙同学
     * @func:空间租赁申请 按时间
     */
    public function spaceRentalApplyByTime($memberInfo)
    {
        global $_W,$_GPC;
        $uid = $memberInfo['uid'];
        $checkArr = array(
            'apply_content'=>'申请事由',
            'apply_name'=>'申请人姓名',
            'apply_mobile'=>'申请人手机号',
            'apply_remark'=>'申请备注',
            'apply_day'=>'申请日',
            'apply_hours'=>'申请时间',
            'space_id'=>'租赁空间id'
        );

        //字段是否为空判断
        $this->commconCheckEmptyFields($_GPC,$checkArr);
        $apply_content = $_GPC['apply_content'];
        $apply_name = $_GPC['apply_name'];
        $apply_mobile = $_GPC['apply_mobile'];
        $apply_remark = $_GPC['apply_remark'];
        $apply_day = $_GPC['apply_day'];
        $apply_hours = $_GPC['apply_hours'];
        $space_id = $_GPC['space_id'];
        $apply_hours = json_decode($apply_hours);

        if(empty($apply_hours)){
            $this->commonApiReturn(-1,'','租赁时间不能为空');
        }

        if(!is_array($apply_hours))
        {
            $this->commonApiReturn(-1,'','租赁时间数据结构必须为array');
        }

        //检测申请的租赁时间节点是否在产品列表里
        $this->checkRentalPro($apply_hours,$space_id);
        //检测订单中是否存在被出租的租赁时间节点
        $this->checkRentalStatus($space_id,$apply_day,$apply_hours);

        //下单
        try{
            pdo_begin();

            $sql = "select * from ".tablename('spacerental_spaceproduct')." where `uniacid`=:uniacid and `id` in (".implode(',',$apply_hours).")";
            $pro_info = pdo_fetchall($sql,array(':uniacid'=>$_W['uniacid']));

            foreach ($pro_info as $k=>$v)
            {
                $pro_names[] = $v['pro_name'];
            }


            $spaceInfo = pdo_fetch("select * from ".tablename('spacerental_space')." where `uniacid`=:uniacid and `id`=:space_id",
                array(
                    ':uniacid'=>$_W['uniacid'],
                    ':space_id'=>$space_id
                ));

            $date = date('YmdHis');
            $uid = $uid;
            $space_id = $space_id;

            //时间精确到秒 空间id 用户uid 合起来组成 订单号
            $orderno = $date.$space_id.$uid;

            $orderData = array(
                'apply_content'=>$apply_content,
                'apply_name'=>$apply_name,
                'apply_mobile'=>$apply_mobile,
                'apply_remark'=>$apply_remark,
                'space_id'=>$space_id,
                'uid'=>$uid,
                'uniacid'=>$_W['uniacid'],
                'day'=>$apply_day,
                'pro_ids'=>implode(',',$apply_hours),
                'pro_names'=>implode(',',$pro_names),
                'space_type'=>$spaceInfo['type_id'],
                'create_time'=>time(),
                'order_no'=>$orderno
//                    'status'=>1, 默认为1 待审核
            );

            $orderInsert = pdo_insert('spacerental_order',$orderData);
            $order_id = pdo_insertid();

            if(!$orderInsert){
                throw new Exception('订单表写入失败');
            }

            //$apply_hours 产品id集合

            foreach ($apply_hours as $key=>$value)
            {
                $sql = "select * from ".tablename('spacerental_spaceproduct')." where `uniacid`=:uniacid and `id`=:pro_id";
                $pro_info = pdo_fetch($sql,array(':uniacid'=>$_W['uniacid'],':pro_id'=>$value));


//                $prostr = str_replace(':00','',$pro_info['pro_name']);
//                $applyday = str_replace('-','',$apply_day);
//
//                $orderno = $applyday.$prostr;
                $orderDetailData = array(
                    'order_id'=>$order_id,      //订单id
//                    'order_no'=>$orderno,       //订单编号
                    'space_id'=>$space_id,      //空间id
                    'pro_name'=>$pro_info['pro_name'],      //商品名称 这里是小时
                    'apply_name'=>$apply_name,  //申请人姓名
                    'apply_mobile'=>$apply_mobile,  //申请人手机号码
                    'uniacid'=>$_W['uniacid'],
                    'pro_id'=>$value,
                    'day'=>$apply_day,

                );

                $orderDetailDataInsert = pdo_insert('spacerental_orderdetail',$orderDetailData);
                if(!$orderDetailDataInsert){
                    throw new Exception('订单详情写入失败');
                }
            }
            pdo_commit();

            $data = array(
                'order_id'=>$order_id
            );
            $this->commonApiReturn(0,$data,'申请成功，等待审核');
        }catch (Exception $e){
            pdo_rollback();
            $this->commonApiReturn(-1,'','申请失败');
        }
    }


    /**
     * @author:橙橙同学
     * @func:租赁申请
     */
    public function doMobileSpaceRentalApply()
    {
        global $_W,$_GPC;
        $openid = $_W['openid'];
        $this->commonCheckOpenid($openid);
        $uid = mc_openid2uid($openid);
        $this->commonCheckUid($uid);

        $memberInfo = $this->getMemberInfoByUid($uid);

        //接受要被预约申请的租赁空间id
        if(!empty($_GPC['space_id']) && is_numeric($_GPC['space_id']))
        {
            $space_id = $_GPC['space_id'];

            //详情页只有状态为1的才能正常访问
            $spaceInfo = pdo_fetch("select * from ".tablename('spacerental_space')." 
                    where `uniacid`=:uniacid and `id`=:id and status=1",
                     array(':uniacid'=>$_W['uniacid'],':id'=>$space_id)
            );
            //判断是否有该空间
            if(!empty($spaceInfo))
            {
                //渲染页面 分为两种 按次数/按时间
                if($spaceInfo['pay_type'] == 1)
                {
                    //按时间收费模板渲染
                    include $this->template('spacerental/spaceapply');
                }
                else{
                    //按照次数收费模板渲染
                    include $this->template('spacerental/spaceapplybytimes');
                }
            }
            else{
                $this->commonTips('没获取到此空间信息','','info');
            }
        }
        else {
            $this->commonTips('没获取到此空间信息','','info');
        }
    }


    public function getSpaceTypeName($type_id)
    {
        global $_W;
        if(!empty($type_id))
        {
            $sql = "select * from ".tablename('spacerental_spacetype')."where `uniacid`=:uniacid and id=:type_id";
            $orderInfo = pdo_fetch($sql,array(':uniacid'=>$_W['uniacid'],':type_id'=>$type_id));
            if(!empty($orderInfo)){
                return $orderInfo['type_name'];
            }else{
                return '无';
            }
        }else{
            return '无';
        }
    }

    public function getSpaceInfoById($space_id)
    {
        global $_W;
        if(!empty($space_id))
        {
            $sql = "select * from ".tablename('spacerental_space')."where `uniacid`=:uniacid and id=:space_id";
            $spaceInfo = pdo_fetch($sql,array(':uniacid'=>$_W['uniacid'],':space_id'=>$space_id));
            return $spaceInfo;
        }else{
            return '';
        }
    }


    /**
     * @author:橙橙同学
     * @func:订单详情
     */
    public function doMobileSpaceRentalOrderDetail()
    {
        global $_W,$_GPC;
        $openid = $_W['openid'];
//        $this->commonCheckOpenid($openid);
        $uid = mc_openid2uid($openid);
        $this->commonCheckUid($uid);
        $memberInfo = $this->getMemberInfoByUid($uid);


        $isCompanyMember = $this->commonCheckCompanyMember($memberInfo);

        if(!$isCompanyMember)
        {
            $this->commonTips('系统检测到您不是企业会员','','info');
        }


        $order_id = $_GPC['order_id'];
        if(!empty($order_id)){

            $sql = "select * from ".tablename('spacerental_order')." where `uniacid`=:uniacid and id=:order_id
                and `uid`=:uid
            ";
            $orderInfo = pdo_fetch($sql,array(':uniacid'=>$_W['uniacid'],':order_id'=>$order_id,':uid'=>$uid));

            if(!empty($orderInfo))
            {
                //今天的时间戳
                $currentDay = date('Y-m-d');
                $currentTimeStamp = strtotime($currentDay);
                $orderTimeStamp = strtotime($orderInfo['day']);

                //如果订单的日期小于今天日期
                if($orderTimeStamp<$currentTimeStamp)
                {
                    //过期了
                    $isexpired = true;
                }

                $sql = "select * from ".tablename('spacerental_space')."where `uniacid`=:uniacid and `id`=:space_id";
                $spaceInfo = pdo_fetch($sql,array(':uniacid'=>$_W['uniacid'],':space_id'=>$orderInfo['space_id']));

                include $this->template('spacerental/orderdetail');
            }else{
                $this->commonTips('未获取到订单信息','','info');
            }
        }else{
            $this->commonTips('未获取到订单信息','','info');

        }
    }



    /**
     * @author:橙橙同学
     * @func:用户申请记录
     */
    public function doMobileSpaceRentalOrderRecord()
    {
        global $_W,$_GPC;
        $openid = $_W['openid'];
//        $this->commonCheckOpenid($openid);
        $uid = mc_openid2uid($openid);
        $this->commonCheckUid($uid);
        $memberInfo = $this->getMemberInfoByUid($uid);
        $this->commonCheckCompanyMember($memberInfo);

        $sql = "select * from ".tablename('spacerental_order')."where `uniacid`=:uniacid and `uid`=:uid order by status desc";
        $orderInfoList = pdo_fetchall($sql,array(':uniacid'=>$_W['uniacid'],':uid'=>$uid));
        include $this->template('spacerental/orderrecord');

    }


	public function doMobileIndexnav() {
		//这个操作被定义用来呈现 功能封面
	}
	public function doWebSpacerentalapplymanage() {
		//这个操作被定义用来呈现 管理中心导航菜单
	}
	public function doMobilePersonalnav() {
		//这个操作被定义用来呈现 微站个人中心导航
	}
	public function doMobileQknav() {
		//这个操作被定义用来呈现 微站快捷功能导航
	}

}